using System.Collections.Generic;
using System.IO;
using Core;

namespace DoItNow.Features
{
	public class ToggleBetweenImplementationAndTestFeature : BindableCommandFeature
	{
        public ToggleBetweenImplementationAndTestFeature(Ide ide, OutputPane outputPane)
			: base("Toggle Implementation And Test", "Text Editor::alt+t", ide, outputPane)
		{
		}

		public override bool IsAvailable
		{
			get { return Ide.ActiveDocument != null; }
		}

		public override void OnExecute()
		{
			CodeDocument document = Ide.ActiveDocument;

			string toggledDocumentName = GetToggledDocumentName(document.FilePath);

			List<string> sourceFiles = Ide.FindFilesInSolution();
			List<string> candidates = sourceFiles.FindAll(match => Path.GetFileNameWithoutExtension(match) == toggledDocumentName);

			string extension = Path.GetExtension(document.FilePath);

			if (candidates.Exists(match => Path.GetExtension(match) == extension))
				candidates.RemoveAll(match => Path.GetExtension(match) != extension);

            if (candidates.Count > 0)
			    Ide.OpenFile(candidates[0]);
		}

		public string GetToggledDocumentName(string path)
		{
			string activeDocumentName = Path.GetFileNameWithoutExtension(path);
			return activeDocumentName.StartsWith("Test") ? activeDocumentName.Remove(0, 4) : "Test" + activeDocumentName;
		}
	}
}